<?php
/**
 * Created by PhpStorm.
 * User: xuewl
 * Date: 2017/12/23
 * Time: 02:40
 */

namespace app\wechat\service;
use app\common\library\Service;

/**
 * Class WechatFans
 * 微信公众号粉丝类
 * @package app\wechat\service
 */
class WechatFans extends Service
{
    use \app\common\library\traits\Model;
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('wechat/wechat_fans');
    }

    /**
     * 更新微信粉丝
     * @param array $info
     * @return boolean
     */
    public function create($info) {
        if(empty($info)) {
            $this->error = "粉丝数据不能为空";
            return false;
        }

        $openid = $this->model
            ->where(['openid' => $info['openid']])
            ->value('openid');

        $where = [];
        if($openid) {
            $where['openid'] = $info['openid'];
        }

        $result = $this->save($info, $where);
        if(!$result) {
            $this->error = $this->getError();
            return false;
        }
        return $result;
    }

    public function getOpenidByUnionid($unionid) {
        return $this->model->where(['unionid' => $unionid])->value('openid');
    }
}